/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sql;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

import java.sql.*;

/**
 *
 * @author luigi
 */
public class SQLite {

    private String url = "jdbc:sqlite:db/jerbo.db";
    
    public SQLite() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void connection() throws ClassNotFoundException, SQLException {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection(url);
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("select * from articles;");
        ResultSetMetaData rsmd = rs.getMetaData();
        while (rs.next()) {
            for (int i = 1; i <= rsmd.getColumnCount(); i++)
                System.out.println(rsmd.getColumnName(i) + " = " + rs.getString(i));
        }
        rs.close();
        conn.close();
    }
    
    @Test
    public void journal() throws SQLException, ClassNotFoundException {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection(url);
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("select time from journal;");
        java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("dd/MM/yy");
        
        while (rs.next()) {
            System.out.println(rs.getLong("time"));
            System.out.println(dateFormat.format(new java.util.Date(rs.getLong("time"))));
        }
        
    }

}